import heapq
from dataclasses import dataclass


@dataclass
class Student:
    name: str
    age: int
    score: int

    def __lt__(self, other):
        return self.score < other.score


# 方法1: 元组排序
student_list = [
    Student("Lili", 18, 99),
    Student("tom", 19, 98),
    Student("jack", 20, 97),
    Student("mali", 21, 80),
    Student("alun", 22, 90),
]

h = []
for it in student_list:
    heapq.heappush(h, (it.score, it))
print(h)

""" 
        [(80, Student(name='mali', age=21, score=80)), 
    (90, Student(name='alun', age=22, score=90)), (98, Student(name='tom', age=19, score=98)), 
(99, Student(name='Lili', age=18, score=99)), (97, Student(name='jack', age=20, score=97))]
"""

# 方法2: 对象排序
h = []
for it in student_list:
    heapq.heappush(h, it)
print(h)
""" 
        Student(name='mali', age=21, score=80), 
    Student(name='alun', age=22, score=90), Student(name='tom', age=19, score=98), 
Student(name='Lili', age=18, score=99), Student(name='jack', age=20, score=97)]
"""
